<?php

include('phpmail/phpmailer.php');
class PublicAction extends Action
{
		
    function login()
    {
        $this->display("Public:login");
    }
    function doLogin()
    {
			$Member = M("user");
			$map['user_email'] = $_POST['email'];
			$map['user_psw'] = md5($_POST['password']);
			$checkUser = $Member->where($map)->find();
			if($checkUser)
            {
				Session::set(C('USER_AUTH_KEY'),$checkUser['user_id']);
				Session::set('user',$checkUser['user_email']);
				if(empty($checkUser['user_last_time']))
				{
					$Member->where("user_id = ".$checkUser['user_id'])->setField("user_last_time",time());
				}
				else
				{
					$Member->where("user_id = ".$checkUser['user_id'])->setField("user_update_time",time());
				}
				echo 1;
			}
            else
            {
                echo 0;
			}
	}	
	function doRegist()
	{
		$user = M("user");
		$data['user_email'] = $_POST['email'];
		$data['user_psw'] = md5($_POST['password']);
		$checkUser = $user->where($data)->find();
		if(empty($checkUser))
		{
			$data['user_time'] = time();
			$addUser = $user->data($data)->add();
			if($addUser)
			{
				$findUser = $user->where('user_id='.$addUser)->find();
				$rbodycontent = file_get_contents(APP_PATH."/Lib/EmailTpl/email_regist_tpl.html");
				
				$table_content = array("[EMAIL]","[PSW]","[DATETIME]");
				
				$replace_content = array($findUser['user_email'],$_POST['password'],date('Y-m-d H:i:s',$findUser['user_time']));
				
				$bodys=str_replace($table_content,$replace_content,$rbodycontent);
				$automail = sendMail("网站新用户注册",$bodys,$findUser['user_email']);	
				if($automail)
				{
					echo 1;
				}
			}
			else
			{
				echo 0;
			}		
		}
		else
		{
			echo 2;
		}
	}
	
	function doFindPsw()
	{
            $email = $_POST['email'];
            $user = M("user");
            $data['user_email'] = $_POST['email'];
            $checkUser = $user->where($data)->find();
            if( $checkUser){
            /**
            * 现在让我们产生一个经过md5加密码过的字符串
            */
                $x = md5($checkUser['user_email'].'+'.$checkUser['user_psw']);
                $String = base64_encode($checkUser['user_email']."丫".$x);
                $url = 'http://'.$_SERVER['SERVER_NAME'];
                
                $rbodycontent = file_get_contents(APP_PATH."/Lib/EmailTpl/email_password_tpl.html");
                $table_content = array("[String]","[url]");
                $replace_content = array($String,$url.__URL__.'/Verification');
                $bodys=str_replace($table_content,$replace_content,$rbodycontent);
                $automail = sendMail("找回密码",$bodys,$email);
                if($automail){
                    echo 3;
                }else{
                    echo 4;
                }
            }else{
                 echo 2;
            }
	}
	
	public function Verification(){
		/**
		 * 用base64_decode解开$_GET['p']的值
		 */
	   $array = explode('丫',base64_decode($_GET['p']));
		/**
		* 这时，我们会得到一个数组，$array，里面分别存放了用户名和我们需要一段字符串
		* $array[0] 邮箱地址
		* $array[1] 为我们生成的字符串
		*/
		
	   $data['user_email'] = $array['0'];
	   $user = M("user");
	   $checkUser = $user->where($data)->find();
		/**
		* 产生配置码 
		*/
		$checkCode = md5($checkUser['user_email'].'+'.$checkUser['user_psw']);
	 
		 if( $array['1'] === $checkCode ){
			//重新设计密码
			$this->assign("id",$checkUser['user_id']);
			$this->display("Public:forgot");
		}else{
			//报错
			$this -> assign("dsp","no");
			$this -> display("Public:forgot");
		}
	}
	
	
	public function doChangePassword(){
		$user = M("user");
		$data['user_psw'] = md5($_POST['repassword']);
		$checkUser = $user->data($data)->where('user_id = '.$_POST['user_id'])->save();
		if($checkUser){
			echo "1";
		}else{
			echo "2";
		}
	}
	
    function verify()
    { 
	$type = isset($_GET['type'])?$_GET['type']:'gif'; 
        import("ORG.Util.Image"); 
        Image::buildImageVerify(4,1,$type,'','20px'); 
    }
    function logout()
    {
		if(Session::is_set(C('USER_AUTH_KEY'))){
			Session::clear();  //Session是类名，::访问改类下的静态成员，或者方法及常量
			$this->assign('jumpUrl',__URL__.'/login');
			$this->success("注销成功！");
		}
        else
        {
			$this->assign('jumpUrl',__URL__.'/login');
			$this->error('已经注销！');
		}
	}

}
?>